package com.xcx.baseManger.until;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ListUtils {
    final private StringRedisTemplate redisTemplate;

    public long rightPushAll(String key, List<String> value) {
        try {
            return redisTemplate.opsForList().rightPushAll(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * @param key   title
     * @param value value
     * @return
     */
    public long rightPush(String key, String value) {
        try {
            return redisTemplate.opsForList().rightPush(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * @param key     title
     * @param timeOut 超时时间s
     * @return
     */
    public Object leftPop(String key, long timeOut) {
        try {
            //返回新增元素后的分数
            return redisTemplate.opsForList().leftPop(key, timeOut, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
